<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li class="active"><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. crypto">1. crypto</a></li><li><a href="#t12. 散列(哈希)算法">2. 散列(哈希)算法</a><ul><li><a href="#t22.1 获取所有的散列算法">2.1 获取所有的散列算法</a></li><li><a href="#t32.2 语法说明">2.2 语法说明</a></li><li><a href="#t42.3 散列算法示例">2.3 散列算法示例</a></li></ul></li><li><a href="#t53. HMAC算法">3. HMAC算法</a><ul><li><a href="#t63.1 语法">3.1 语法</a></li><li><a href="#t73.2 生成私钥">3.2 生成私钥</a></li><li><a href="#t83.3 示例">3.3 示例</a></li></ul></li><li><a href="#t94. 对称加密">4. 对称加密</a></li><li><a href="#t105. 非对称加密算法">5. 非对称加密算法</a></li><li><a href="#t116. 签名">6. 签名</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. crypto">1. crypto <a href="#t01. crypto"> # </a></h2>
<p><code>crypto</code>&#x662F;<code>node.js</code>&#x4E2D;&#x5B9E;&#x73B0;&#x52A0;&#x5BC6;&#x548C;&#x89E3;&#x5BC6;&#x7684;&#x6A21;&#x5757;
&#x5728;<code>node.js</code>&#x4E2D;&#xFF0C;&#x4F7F;&#x7528;<code>OpenSSL</code>&#x7C7B;&#x5E93;&#x4F5C;&#x4E3A;&#x5185;&#x90E8;&#x5B9E;&#x73B0;&#x52A0;&#x5BC6;&#x89E3;&#x5BC6;&#x7684;&#x624B;&#x6BB5;
<code>OpenSSL</code>&#x662F;&#x4E00;&#x4E2A;&#x7ECF;&#x8FC7;&#x4E25;&#x683C;&#x6D4B;&#x8BD5;&#x7684;&#x53EF;&#x9760;&#x7684;&#x52A0;&#x5BC6;&#x4E0E;&#x89E3;&#x5BC6;&#x7B97;&#x6CD5;&#x7684;&#x5B9E;&#x73B0;&#x5DE5;&#x5177;</p>
<p><a href="http://dl.pconline.com.cn/download/355862-1.html">windows&#x7248;openSSL&#x4E0B;&#x8F7D;</a></p>
<h2 id="t12. &#x6563;&#x5217;(&#x54C8;&#x5E0C;)&#x7B97;&#x6CD5;">2. &#x6563;&#x5217;(&#x54C8;&#x5E0C;)&#x7B97;&#x6CD5; <a href="#t12. &#x6563;&#x5217;(&#x54C8;&#x5E0C;)&#x7B97;&#x6CD5;"> # </a></h2>
<p>&#x6563;&#x5217;&#x7B97;&#x6CD5;&#x4E5F;&#x53EB;&#x54C8;&#x5E0C;&#x7B97;&#x6CD5;&#xFF0C;&#x7528;&#x6765;&#x628A;&#x4EFB;&#x610F;&#x957F;&#x5EA6;&#x7684;&#x8F93;&#x5165;&#x53D8;&#x6362;&#x6210;&#x56FA;&#x5B9A;&#x957F;&#x5EA6;&#x7684;&#x8F93;&#x51FA;,&#x5E38;&#x89C1;&#x7684;&#x6709;md5,sha1&#x7B49;</p>
<ul>
<li>&#x76F8;&#x540C;&#x7684;&#x8F93;&#x5165;&#x4F1A;&#x4EA7;&#x751F;&#x76F8;&#x540C;&#x7684;&#x8F93;&#x51FA;</li>
<li>&#x4E0D;&#x540C;&#x7684;&#x8F93;&#x51FA;&#x4F1A;&#x4EA7;&#x751F;&#x4E0D;&#x540C;&#x7684;&#x8F93;&#x51FA;</li>
<li>&#x4EFB;&#x610F;&#x7684;&#x8F93;&#x5165;&#x957F;&#x5EA6;&#x8F93;&#x51FA;&#x957F;&#x5EA6;&#x662F;&#x76F8;&#x540C;&#x7684;</li>
<li>&#x4E0D;&#x80FD;&#x4ECE;&#x8F93;&#x51FA;&#x63A8;&#x7B97;&#x51FA;&#x8F93;&#x5165;&#x7684;&#x503C;</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/md5.jpg" alt="md5"></p>
<h3 id="t22.1 &#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;&#x6563;&#x5217;&#x7B97;&#x6CD5;">2.1 &#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;&#x6563;&#x5217;&#x7B97;&#x6CD5; <a href="#t22.1 &#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;&#x6563;&#x5217;&#x7B97;&#x6CD5;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(crypto.getHashes());
</code></pre>
<h3 id="t32.2 &#x8BED;&#x6CD5;&#x8BF4;&#x660E;">2.2 &#x8BED;&#x6CD5;&#x8BF4;&#x660E; <a href="#t32.2 &#x8BED;&#x6CD5;&#x8BF4;&#x660E;"> # </a></h3>
<pre><code class="lang-javascript">crypto.createHash(algorithm);<span class="hljs-comment">//&#x521B;&#x5EFA;HASH&#x5BF9;&#x8C61;</span>
hash.update(data,[input_encoding]);<span class="hljs-comment">//&#x589E;&#x52A0;&#x8981;&#x6DFB;&#x52A0;&#x6458;&#x8981;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x6458;&#x8981;&#x8F93;&#x51FA;&#x524D;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x591A;&#x6B21;update</span>
hash.digest([encoding]);<span class="hljs-comment">//&#x8F93;&#x51FA;&#x6458;&#x8981;&#x5185;&#x5BB9;&#xFF0C;&#x8F93;&#x51FA;&#x540E;&#x5219;&#x4E0D;&#x80FD;&#x518D;&#x6DFB;&#x52A0;&#x6458;&#x8981;&#x5185;&#x5BB9;</span>
</code></pre>
<h3 id="t42.3 &#x6563;&#x5217;&#x7B97;&#x6CD5;&#x793A;&#x4F8B;">2.3 &#x6563;&#x5217;&#x7B97;&#x6CD5;&#x793A;&#x4F8B; <a href="#t42.3 &#x6563;&#x5217;&#x7B97;&#x6CD5;&#x793A;&#x4F8B;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;crypto&apos;</span>);
<span class="hljs-keyword">var</span> md5 = crypto.createHash(<span class="hljs-string">&apos;md5&apos;</span>);<span class="hljs-comment">//&#x8FD4;&#x56DE;&#x54C8;&#x5E0C;&#x7B97;&#x6CD5;</span>
<span class="hljs-keyword">var</span> md5Sum = md5.update(<span class="hljs-string">&apos;hello&apos;</span>);<span class="hljs-comment">//&#x6307;&#x5B9A;&#x8981;&#x6458;&#x8981;&#x7684;&#x539F;&#x59CB;&#x5185;&#x5BB9;,&#x53EF;&#x4EE5;&#x5728;&#x6458;&#x8981;&#x88AB;&#x8F93;&#x51FA;&#x4E4B;&#x524D;&#x4F7F;&#x7528;&#x591A;&#x6B21;update&#x65B9;&#x6CD5;&#x6765;&#x6DFB;&#x52A0;&#x6458;&#x8981;&#x5185;&#x5BB9;</span>
<span class="hljs-keyword">var</span> result = md5Sum.digest(<span class="hljs-string">&apos;hex&apos;</span>);<span class="hljs-comment">//&#x6458;&#x8981;&#x8F93;&#x51FA;&#xFF0C;&#x5728;&#x4F7F;&#x7528;digest&#x65B9;&#x6CD5;&#x4E4B;&#x540E;&#x4E0D;&#x80FD;&#x518D;&#x5411;hash&#x5BF9;&#x8C61;&#x8FFD;&#x52A0;&#x6458;&#x8981;&#x5185;&#x5BB9;&#x3002;</span>
<span class="hljs-built_in">console</span>.log(result);
</code></pre>
<p>&#x591A;&#x6B21;update</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">var</span> shasum = crypto.createHash(<span class="hljs-string">&apos;sha1&apos;</span>);<span class="hljs-comment">//&#x8FD4;&#x56DE;sha1&#x54C8;&#x5E0C;&#x7B97;&#x6CD5;</span>
<span class="hljs-keyword">var</span> rs = fs.createReadStream(<span class="hljs-string">&apos;./readme.txt&apos;</span>);
rs.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    shasum.update(data);<span class="hljs-comment">//&#x6307;&#x5B9A;&#x8981;&#x6458;&#x8981;&#x7684;&#x539F;&#x59CB;&#x5185;&#x5BB9;,&#x53EF;&#x4EE5;&#x5728;&#x6458;&#x8981;&#x88AB;&#x8F93;&#x51FA;&#x4E4B;&#x524D;&#x4F7F;&#x7528;&#x591A;&#x6B21;update&#x65B9;&#x6CD5;&#x6765;&#x6DFB;&#x52A0;&#x6458;&#x8981;&#x5185;&#x5BB9;</span>
});
rs.on(<span class="hljs-string">&apos;end&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> result = shasum.digest(<span class="hljs-string">&apos;hex&apos;</span>);<span class="hljs-comment">//&#x6458;&#x8981;&#x8F93;&#x51FA;&#xFF0C;&#x5728;&#x4F7F;&#x7528;digest&#x65B9;&#x6CD5;&#x4E4B;&#x540E;&#x4E0D;&#x80FD;&#x518D;&#x5411;hash&#x5BF9;&#x8C61;&#x8FFD;&#x52A0;&#x6458;&#x8981;&#x5185;&#x5BB9;&#x3002;</span>
    <span class="hljs-built_in">console</span>.log(result);
})
</code></pre>
<h2 id="t53. HMAC&#x7B97;&#x6CD5;">3. HMAC&#x7B97;&#x6CD5; <a href="#t53. HMAC&#x7B97;&#x6CD5;"> # </a></h2>
<p>HMAC&#x7B97;&#x6CD5;&#x5C06;&#x6563;&#x5217;&#x7B97;&#x6CD5;&#x4E0E;&#x4E00;&#x4E2A;&#x5BC6;&#x94A5;&#x7ED3;&#x5408;&#x5728;&#x4E00;&#x8D77;&#xFF0C;&#x4EE5;&#x963B;&#x6B62;&#x5BF9;&#x7B7E;&#x540D;&#x5B8C;&#x6574;&#x6027;&#x7684;&#x7834;&#x574F;</p>
<p><img src="http://img.zhufengpeixun.cn/hmac.jpg" alt="hmac"></p>
<h3 id="t63.1 &#x8BED;&#x6CD5;">3.1 &#x8BED;&#x6CD5; <a href="#t63.1 &#x8BED;&#x6CD5;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hmac crypto.createHmac(algorithm,key);
hmac.update(data);
</code></pre>
<ul>
<li>algorithm &#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x7528;&#x7684;&#x6458;&#x8981;&#x7B97;&#x6CD5;&#xFF0C;&#x4F8B;&#x5982; sha1&#x3001;md5&#x3001;sha256</li>
<li>key&#x4E3A;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;PEM&#x683C;&#x5F0F;&#x7684;&#x5BC6;&#x94A5;</li>
</ul>
<h3 id="t73.2 &#x751F;&#x6210;&#x79C1;&#x94A5;">3.2 &#x751F;&#x6210;&#x79C1;&#x94A5; <a href="#t73.2 &#x751F;&#x6210;&#x79C1;&#x94A5;"> # </a></h3>
<p>PEM&#x662F;OpenSSL&#x7684;&#x6807;&#x51C6;&#x683C;&#x5F0F;&#xFF0C;OpenSSL&#x4F7F;&#x7528;PEM&#x6587;&#x4EF6;&#x683C;&#x5F0F;&#x5B58;&#x50A8;&#x8BC1;&#x4E66;&#x548C;&#x5BC6;&#x94A5;&#xFF0C;&#x662F;&#x57FA;&#x4E8E;Base64&#x7F16;&#x7801;&#x7684;&#x8BC1;&#x4E66;&#x3002;</p>
<pre><code class="lang-javascript">$ openssl genrsa -out rsa_private.key <span class="hljs-number">1024</span>
</code></pre>
<h3 id="t83.3 &#x793A;&#x4F8B;">3.3 &#x793A;&#x4F8B; <a href="#t83.3 &#x793A;&#x4F8B;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> pem = fs.readFileSync(path.join(__dirname, <span class="hljs-string">&apos;./rsa_private.key&apos;</span>));
<span class="hljs-keyword">let</span> key = pem.toString(<span class="hljs-string">&apos;ascii&apos;</span>);
<span class="hljs-keyword">let</span> hmac = crypto.createHmac(<span class="hljs-string">&apos;sha1&apos;</span>, key);
<span class="hljs-keyword">let</span> rs = fs.createReadStream(path.join(__dirname, <span class="hljs-string">&apos;./1.txt&apos;</span>));
rs.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    hmac.update(data);
});
rs.on(<span class="hljs-string">&apos;end&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> result = hmac.digest(<span class="hljs-string">&apos;hex&apos;</span>);
    <span class="hljs-built_in">console</span>.log(result);
});
</code></pre>
<h2 id="t94. &#x5BF9;&#x79F0;&#x52A0;&#x5BC6;">4. &#x5BF9;&#x79F0;&#x52A0;&#x5BC6; <a href="#t94. &#x5BF9;&#x79F0;&#x52A0;&#x5BC6;"> # </a></h2>
<ul>
<li>blowfish&#x7B97;&#x6CD5;&#x662F;&#x4E00;&#x79CD;&#x5BF9;&#x79F0;&#x7684;&#x52A0;&#x5BC6;&#x7B97;&#x6CD5;,&#x5BF9;&#x79F0;&#x7684;&#x610F;&#x601D;&#x5C31;&#x662F;&#x52A0;&#x5BC6;&#x548C;&#x89E3;&#x5BC6;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x5BC6;&#x94A5;&#x3002;</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/encry.jpg" alt="encry"></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;crypto&apos;</span>);
<span class="hljs-keyword">var</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> str = <span class="hljs-string">&apos;hello&apos;</span>;
<span class="hljs-keyword">let</span> cipher = crypto.createCipher(<span class="hljs-string">&apos;blowfish&apos;</span>, fs.readFileSync(path.join(__dirname, <span class="hljs-string">&apos;rsa_private.key&apos;</span>)));
<span class="hljs-keyword">let</span> encry = cipher.update(str, <span class="hljs-string">&apos;utf8&apos;</span>,<span class="hljs-string">&apos;hex&apos;</span>);
encry += cipher.final(<span class="hljs-string">&apos;hex&apos;</span>);
<span class="hljs-built_in">console</span>.log(encry);

<span class="hljs-keyword">let</span> deciper = crypto.createDecipher(<span class="hljs-string">&apos;blowfish&apos;</span>, fs.readFileSync(path.join(__dirname, <span class="hljs-string">&apos;rsa_private.key&apos;</span>)));
<span class="hljs-keyword">let</span> deEncry = deciper.update(encry, <span class="hljs-string">&apos;hex&apos;</span>,<span class="hljs-string">&apos;utf8&apos;</span>);
deEncry += deciper.final(<span class="hljs-string">&apos;utf8&apos;</span>);
<span class="hljs-built_in">console</span>.log(deEncry);
</code></pre>
<h2 id="t105. &#x975E;&#x5BF9;&#x79F0;&#x52A0;&#x5BC6;&#x7B97;&#x6CD5;">5. &#x975E;&#x5BF9;&#x79F0;&#x52A0;&#x5BC6;&#x7B97;&#x6CD5; <a href="#t105. &#x975E;&#x5BF9;&#x79F0;&#x52A0;&#x5BC6;&#x7B97;&#x6CD5;"> # </a></h2>
<ul>
<li>&#x975E;&#x5BF9;&#x79F0;&#x52A0;&#x5BC6;&#x7B97;&#x6CD5;&#x9700;&#x8981;&#x4E24;&#x4E2A;&#x5BC6;&#x94A5;&#xFF1A;&#x516C;&#x5F00;&#x5BC6;&#x94A5;(publickey)&#x548C;&#x79C1;&#x6709;&#x5BC6;&#x94A5;(privatekey)</li>
<li>&#x516C;&#x94A5;&#x4E0E;&#x79C1;&#x94A5;&#x662F;&#x4E00;&#x5BF9;&#xFF0C;&#x5982;&#x679C;&#x7528;&#x516C;&#x94A5;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x52A0;&#x5BC6;&#xFF0C;&#x53EA;&#x6709;&#x7528;&#x5BF9;&#x5E94;&#x7684;&#x79C1;&#x94A5;&#x624D;&#x80FD;&#x89E3;&#x5BC6;,&#x5982;&#x679C;&#x79C1;&#x94A5;&#x52A0;&#x5BC6;&#xFF0C;&#x53EA;&#x80FD;&#x516C;&#x94A5;&#x89E3;&#x5BC6;</li>
<li>&#x56E0;&#x4E3A;&#x52A0;&#x5BC6;&#x548C;&#x89E3;&#x5BC6;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x5BC6;&#x94A5;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x79CD;&#x7B97;&#x6CD5;&#x53EB;&#x4F5C;&#x975E;&#x5BF9;&#x79F0;&#x52A0;&#x5BC6;&#x7B97;&#x6CD5;</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/rsa.jpg" alt="rsa"></p>
<p>&#x4E3A;&#x79C1;&#x94A5;&#x521B;&#x5EFA;&#x516C;&#x94A5;</p>
<pre><code class="lang-javascript">openssl rsa -<span class="hljs-keyword">in</span> rsa_private.key -pubout -out rsa_public.key
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;crypto&apos;</span>);
<span class="hljs-keyword">var</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> key = fs.readFileSync(path.join(__dirname, <span class="hljs-string">&apos;rsa_private.key&apos;</span>));
<span class="hljs-keyword">let</span> cert = fs.readFileSync(path.join(__dirname, <span class="hljs-string">&apos;rsa_public.key&apos;</span>));
<span class="hljs-keyword">let</span> secret = crypto.publicEncrypt(cert, buffer);<span class="hljs-comment">//&#x516C;&#x94A5;&#x52A0;&#x5BC6;</span>
<span class="hljs-keyword">let</span> result = crypto.privateDecrypt(key, secret);<span class="hljs-comment">//&#x79C1;&#x94A5;&#x89E3;&#x5BC6;</span>
<span class="hljs-built_in">console</span>.log(result.toString());
</code></pre>
<h2 id="t116. &#x7B7E;&#x540D;">6. &#x7B7E;&#x540D; <a href="#t116. &#x7B7E;&#x540D;"> # </a></h2>
<p>&#x5728;&#x7F51;&#x7EDC;&#x4E2D;&#xFF0C;&#x79C1;&#x94A5;&#x7684;&#x62E5;&#x6709;&#x8005;&#x53EF;&#x4EE5;&#x5728;&#x4E00;&#x6BB5;&#x6570;&#x636E;&#x88AB;&#x53D1;&#x9001;&#x4E4B;&#x524D;&#x5148;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;<strong>&#x7B7E;&#x540D;</strong>&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x7B7E;&#x540D;
&#x901A;&#x8FC7;&#x7F51;&#x7EDC;&#x628A;&#x6B64;&#x6570;&#x636E;&#x53D1;&#x9001;&#x7ED9;&#x6570;&#x636E;&#x63A5;&#x6536;&#x8005;&#x4E4B;&#x540E;&#xFF0C;&#x6570;&#x636E;&#x7684;&#x63A5;&#x6536;&#x8005;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<strong>&#x516C;&#x94A5;</strong>&#x6765;&#x5BF9;&#x8BE5;&#x7B7E;&#x540D;&#x8FDB;&#x884C;&#x9A8C;&#x8BC1;,&#x4EE5;&#x786E;&#x4FDD;&#x8FD9;&#x6BB5;&#x6570;&#x636E;&#x662F;&#x79C1;&#x94A5;&#x7684;&#x62E5;&#x6709;&#x8005;&#x6240;&#x53D1;&#x51FA;&#x7684;&#x539F;&#x59CB;&#x6570;&#x636E;&#xFF0C;&#x4E14;&#x5728;&#x7F51;&#x7EDC;&#x4E2D;&#x7684;&#x4F20;&#x8F93;&#x8FC7;&#x7A0B;&#x4E2D;&#x672A;&#x88AB;&#x4FEE;&#x6539;&#x3002;</p>
<p><img src="http://img.zhufengpeixun.cn/sign.jpg" alt="sign"></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> private = fs.readFileSync(path.join(__dirname, <span class="hljs-string">&apos;rsa_private.key&apos;</span>), <span class="hljs-string">&apos;ascii&apos;</span>);
<span class="hljs-keyword">let</span> public = fs.readFileSync(path.join(__dirname, <span class="hljs-string">&apos;rsa_public.key&apos;</span>), <span class="hljs-string">&apos;ascii&apos;</span>);
<span class="hljs-keyword">let</span> str = <span class="hljs-string">&apos;zhufengpeixun&apos;</span>;
<span class="hljs-keyword">let</span> sign = crypto.createSign(<span class="hljs-string">&apos;RSA-SHA256&apos;</span>);
sign.update(str);
<span class="hljs-keyword">let</span> signed = sign.sign(private, <span class="hljs-string">&apos;hex&apos;</span>);
<span class="hljs-keyword">let</span> verify = crypto.createVerify(<span class="hljs-string">&apos;RSA-SHA256&apos;</span>);
verify.update(str);
<span class="hljs-keyword">let</span> verifyResult = verify.verify(public,signed,<span class="hljs-string">&apos;hex&apos;</span>); <span class="hljs-comment">//true</span>
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
